﻿namespace fastdb.Common;

internal class ByteHelper
{
    public static byte[] ByteArrayJoin(params byte[][] datas)
    {
        var len = datas.Sum(t => t.Length);

        var res = new byte[len];
        for (var i = 0; i < datas.Length; i++) datas[i].CopyTo(res, i == 0 ? 0 : datas[i - 1].Length);

        return res;
    }


    public static byte[] GetBytesFromNumber(int no)
    {
        var res = BitConverter.GetBytes(no);
        if (BitConverter.IsLittleEndian)
            Array.Reverse(res);

        return res;
    }

    public static int GetNumberFromBytes(byte[] bytes)
    {
        if (BitConverter.IsLittleEndian)
            Array.Reverse(bytes);

        var res = BitConverter.ToInt32(bytes);
        return res;
    }
}